﻿// thread_1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <thread>
#include <chrono>
#include <mutex>                // std::mutex, std::unique_lock
#include <condition_variable>    // std::condition_variable
#include <vector>
#include <iomanip>
#include <sstream>
#include "smartthread.h"

using namespace std;

int stFunc() {

	chrono::system_clock clock;
	int64_t ms = chrono::duration_cast<chrono::milliseconds>(
		clock.now().time_since_epoch()).count();

	//    std::cout<<"stFunc run,current time ms:"<<ms<<std::endl;
	cout << "stFunc run,current tims ms:" << ms<<endl;

	std::this_thread::sleep_for(
		std::chrono::milliseconds(
			100));
	return 0;
}

int main()
{
	cout << "main ---- " << endl;

	cout << "main---" <<endl;

	std::ostringstream oss;
	oss << std::this_thread::get_id();
	std::string stid = oss.str();
	unsigned long long tid = std::stoull(stid);
	cout << "main tid:" << tid << endl;
	//子线程设置执行函数
	SmartThread::instance()->setFunc(stFunc);
	//子线程函数执行500毫秒
	std::this_thread::sleep_for(
		std::chrono::milliseconds(500));
	//子线程被阻塞
	cout << "block smart thread;" <<endl;
	SmartThread::instance()->block();

	std::this_thread::sleep_for(
		std::chrono::milliseconds(500));
	//子线程被唤醒
	cout << "notify smart thread."<<endl;
	SmartThread::instance()->notify();
	//主线程继续运行2秒
	std::this_thread::sleep_for(
		std::chrono::milliseconds(2000));

	return 0;
}

